package cannot.controller;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cannot.pojo.MainType;
import cannot.pojo.OtherType;
import cannot.pojo.Tv;
import cannot.service.MainTypeService;
import cannot.service.OtherTypeService;
import cannot.service.TvService;
/**
 * 首页加载控制器
 * @author Administrator
 *
 */
@Controller
public class ForeIndexController {
	@Autowired
	MainTypeService mainTypeService;
	@Autowired
	TvService tvService;
	@Autowired
	OtherTypeService otherTypeService;

	/**
	 * 根据首页的分区id进行数据加载
	 */
	@RequestMapping("fore_index_loadCategory")
	@ResponseBody
	public Object foreIndexLoadCategory(Integer id){
		MainType mainType = mainTypeService.selectById(id);
		//加载图片电影
		List<Tv> tvsImage = tvService.listTvImagesIndex(id);
		//加载链接电影
		List<Tv> tvsUrl =tvService.listTvUrlIndex(id);
		//随机生成其下的一个子分类
		List<OtherType> otherTypes= otherTypeService.listByMainType(id);
		Integer otherTypeId = getRandomOtherType(otherTypes);
		//加载子分类
		OtherType otherType = otherTypeService.selectOtherType(otherTypes.get(otherTypeId).getId());
		//加载子分类下的影片
		List<Tv> tvsOtherType = tvService.listTvOtherType(otherTypes.get(otherTypeId).getId());
		mainType.setTvsImage(tvsImage);
		mainType.setTvsUrl(tvsUrl);
		mainType.setTvsOtherType(tvsOtherType);
		mainType.setOtherType(otherType);
		return mainType;
	}
	
	
	/**
	 * 给入一个子分类list，返回一个随机的子分类的id
	 */
	private Integer getRandomOtherType(List<OtherType> otherTypes){
		Random random = new Random();
		Integer i =  random.nextInt(otherTypes.size());
		return i;
	}
	
}
